package mediateca.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import mediateca.action.beans.Author;
import mediateca.action.beans.Book;
import mediateca.action.utils.JsonUtils;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

public class BookAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1314543683124218897L;
	private String imageFile;
	private String title;	

	private String editorialSelected;
	private List<Author> authorsList;
	private LinkedList<String> datesList;	
	private HashMap<String, String> languageList;
	private HashMap<String, String> formatsList;
	public LinkedList<String> getDatesList() {
		return datesList;
	}

	public void setDatesList(LinkedList<String> datesList) {
		this.datesList = datesList;
	}

	public HashMap<String, String> getLanguageList() {
		return languageList;
	}

	public void setLanguageList(HashMap<String, String> languageList) {
		this.languageList = languageList;
	}

	public HashMap<String, String> getFormatsList() {
		return formatsList;
	}

	public void setFormatsList(HashMap<String, String> formatsList) {
		this.formatsList = formatsList;
	}

	private ArrayList<String> authorsSelected;
	// private String[] newAuthors;
	private String themeSelected;
	private String[] newAuthors;
	private HashMap<String, String> authors;
	private HashMap<String, String> editorials;
	private HashMap<String, String> thems;
	private Book newBook;

	public String getImageFile() {
		return imageFile;
	}

	public void setImageFile(String imageFile) {
		this.imageFile = imageFile;
	}	

	public String getTitle() {
		return title;
	}
	//@RequiredStringValidator(key="title.requiredstring")
	public void setTitle(String title) {
		this.newBook.setTitle(title);
	}

	public String getThemeSelected() {
		return themeSelected;
	}

	public void setThemeSelected(String themeSelected) {
		this.themeSelected = themeSelected;
	}

	public HashMap<String, String> getAuthors() {
		return authors;
	}

	public void HashMap(HashMap<String, String> authors) {
		this.authors = authors;
	}

	
	 public ArrayList<String> getAuthorsSelected() { 
		 return	 authorsSelected; 
		 }
	

	public void setAuthorsSelected(String key) {
		key = key.trim();
		String[] campos = key.split(",");
		authorsSelected = new ArrayList<String>();
		for (int j = 0; j < campos.length; j++) {
			this.authorsSelected.add(campos[j].toString());
		}
	}

	public HashMap<String, String> getEditorials() {
		return editorials;
	}

	public void setEditorials(HashMap<String, String> editorials) {
		this.editorials = editorials;
	}

	public String[] getNewAuthors() {
		return newAuthors;
	}

	public void setNewAuthors(String[] newAuthors) {
		this.newAuthors = newAuthors;
	}

	public String getEditorialSelected() {
		return editorialSelected;
	}

	public void setEditorialSelected(String editorial) {
		this.editorialSelected = editorial;
	}

	public Book getNewBook() {
		return newBook;
	}

	public void setNewBook(Book newBook) {
		this.newBook = newBook;
	}	

	// public List<Author> getAuthors() {
	// return authors;
	// }
	//
	// public void setAuthors(List<Author> authors) {
	// this.authors = authors;
	// }

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	@Action(value = "/addBook", results = { @Result(name = "success", location = "addBook", type = "tiles"),
			 								@Result(name = "input", location = "addBook", type = "tiles")})
	public String addBook() throws Exception {
		
		this.title="Los juegos del hambre";
		authorsSelected=new ArrayList<String>();
		this.authorsSelected.add("2");
		//String json = JsonUtils.fromJavaToJson(authorsSelected);
		
		authorsList = new ArrayList<Author>();
		authorsList.add(new Author("1", "Alberto", "Vazquez Figueroa"));
		authorsList.add(new Author("2", "Javier", "Sierra"));
		authorsList.add(new Author("3", "Iker", "Jimenez"));
		authorsList.add(new Author("4", "Santiago", "Camacho"));

		authors = new HashMap<String, String>();
		for (Author currentAuthor : authorsList) {
			authors.put(currentAuthor.getId(), currentAuthor.getCompleteName());
		}
		editorials = new HashMap<String, String>();
		editorials.put("1", "Plaza y Janés");
		editorials.put("2", "Edaf");
		editorials.put("3", "Ediciones B");

		thems = new HashMap<String, String>();
		thems.put("1", "Terror");
		thems.put("2", "Drama");
		thems.put("3", "Comedia");
		
		formatsList = new HashMap<String, String>();
		formatsList.put("1", "butxaca");
		formatsList.put("2", "tapa dura");
		
		languageList = new HashMap<String, String>();
		languageList.put("cat", "Català");
		languageList.put("esp", "Castellà");
		
		Calendar c = new GregorianCalendar();
		String annio = Integer.toString(c.get(Calendar.YEAR));
		
		datesList = new LinkedList<String>();
		for(int i=1900;i<Integer.valueOf(annio);i++){
			datesList.add(Integer.toString(i));
		}
		
		
		return SUCCESS;
	}

	// @Action(value = "/saveBook", interceptorRefs=
	// {@InterceptorRef("fileUploadStack")}, results = { @Result(name =
	// "success", location = "addBook", type = "redirect") })
	@Action(value = "/saveBook", results = { @Result(name = "success", location = "addBook", type = "redirect"),
											 @Result(name = "input", location = "addBook", type = "redirect")})
	public String saveBook() throws Exception {
		/*
		 * TODO recorrer els authorsselected per mirar si n'hi ha de nous
		 * convertint els valors a ints si peta fer un split # per extreure el
		 * nou cognom i nom del autor fer el save del nou author i així
		 * conseguir la seva id. Amb les editorials, tot i que sol en pot haver
		 * una, fer el mateix. Si el que es rep no es pot convertir a int es que
		 * es una de nova. La imatge es un String que es guarda a la BD que sera el src del img.
		 */
		

		try {
			
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			return INPUT;
		}

		return SUCCESS;
	}

	public HashMap<String, String> getThems() {
		return thems;
	}

	@Action(value = "/addAuthorAjax", results = { @Result(name = "success", location = "/pages/books/authorForm.jsp") })
	public String addAuthorAjax() throws Exception {
		return SUCCESS;
	}

	@Action(value = "/addEditorialAjax", results = { @Result(name = "success", location = "/pages/books/editorialForm.jsp") })
	public String addEditorialAjax() throws Exception {
		return SUCCESS;
	}

	@Action(value = "/modify", results = { @Result(name = "success", location = "modify", type = "tiles") })
	public String modify() throws Exception {
		return SUCCESS;
	}

	@Action(value = "/addBookAjax", results = { @Result(name = "success", location = "/pages/books/bookForm.jsp") })
	public String addBookAjax() throws Exception {
		return SUCCESS;
	}	
	@Action(value = "/modifyBookAjax", results = { @Result(name = "success", location = "/pages/books/bookForm.jsp") })
	public String modifyBookAjax() throws Exception {
		return SUCCESS;
	}	

}
